package lion.blog.service;

import java.util.List;

import lion.blog.model.BlogArticle;
import lion.blog.model.BlogCatelog;
import lion.blog.model.BlogMenu;
import lion.blog.model.BlogTag;
import lion.dev.lang.MapJ;
import lion.framework.core.bean.anno.Bean;
import lion.framework.core.cache.CacheManager;
import lion.framework.core.cache.ICache;
import lion.framework.core.db.SessionFactory;

/**
 * @author lion
 * @email hl_0074@sina.com
 * @date 2015年9月21日
 */
@Bean("blog.BlogService")
public class BlogService {

	ICache cache = CacheManager.getCache("blog");
	public static final String BLOG_CATELOG_CACHE_PREFIX = "blog_catelog_cache_prefix_";
	public static final String BLOG_ARTICLE_CACHE_PREFIX = "blog_article_cache_prefix_";
	public static final String BLOG_TAG_CACHE_PREFIX = "blog_tag_cache_prefix_";
	public static final String BLOG_MENU_CACHE_PREFIX = "blog_menu_cache_prefix_";

	public BlogCatelog getBlogCatelog(int catelogId) {

		BlogCatelog catelog = (BlogCatelog) cache.get(BLOG_CATELOG_CACHE_PREFIX + catelogId);
		if (catelog == null) {
			catelog = BlogCatelog.load(catelogId);
			if (catelog != null) {
				cache.put(BLOG_CATELOG_CACHE_PREFIX + catelogId, catelog);
				cache.put(BLOG_CATELOG_CACHE_PREFIX + catelog.getCode(), catelog);
			}
		}

		return catelog;
	}

	public BlogCatelog getBlogCatelog(String code) {

		BlogCatelog catelog = (BlogCatelog) cache.get(BLOG_CATELOG_CACHE_PREFIX + code);
		if (catelog == null) {
			MapJ param = new MapJ();
			param.put("code", code);
			catelog = BlogCatelog.load("where code=:code", param, SessionFactory.getCurrentDB());
			if (catelog != null) {
				cache.put(BLOG_CATELOG_CACHE_PREFIX + catelog.getId(), catelog);
				cache.put(BLOG_CATELOG_CACHE_PREFIX + catelog.getCode(), catelog);
			}
		}

		return catelog;
	}

	public BlogArticle getBlogArticle(String alias) {

		BlogArticle article = (BlogArticle) this.cache.get(BLOG_ARTICLE_CACHE_PREFIX + alias);
		if (article == null) {
			MapJ param = new MapJ();
			param.put("alias", alias);
			article = BlogArticle.load("where alias=:alias", param, SessionFactory.getCurrentDB());

			if (article != null) {
				cache.put(BLOG_ARTICLE_CACHE_PREFIX + article.getId(), article);
				cache.put(BLOG_ARTICLE_CACHE_PREFIX + article.getAlias(), article);
			}
		}
		return article;
	}

	public BlogArticle getBlogArticle(int id) {

		BlogArticle article = (BlogArticle) this.cache.get(BLOG_ARTICLE_CACHE_PREFIX + id);
		if (article == null) {
			article = BlogArticle.load(id);
			if (article != null) {
				cache.put(BLOG_ARTICLE_CACHE_PREFIX + article.getId(), article);
				cache.put(BLOG_ARTICLE_CACHE_PREFIX + article.getAlias(), article);
			}
		}
		return article;
	}

	@SuppressWarnings("unchecked")
	public List<BlogCatelog> getBlogCatelogs() {

		List<BlogCatelog> catelogs = (List<BlogCatelog>) cache.get(BLOG_CATELOG_CACHE_PREFIX + "all_list");
		if (catelogs == null) {
			catelogs = BlogCatelog.list(-1);
			cache.put(BLOG_CATELOG_CACHE_PREFIX + "all_list", catelogs);
		}
		return catelogs;
	}

	@SuppressWarnings("unchecked")
	public List<BlogTag> getBlogTags() {

		List<BlogTag> tags = (List<BlogTag>) cache.get(BLOG_CATELOG_CACHE_PREFIX + "all_list");
		if (tags == null) {
			tags = BlogTag.list(-1);
			cache.put(BLOG_TAG_CACHE_PREFIX + "all_list", tags);
		}
		return tags;
	}

	@SuppressWarnings("unchecked")
	public List<BlogMenu> getBlogMenus() {

		List<BlogMenu> menus = (List<BlogMenu>) cache.get(BLOG_MENU_CACHE_PREFIX + "all_list");

		if (menus == null) {
			menus = BlogMenu.list(-1);
			cache.put(BLOG_MENU_CACHE_PREFIX + "all_list", menus);
		}
		return menus;
	}
}
